(function($){
	//这个是validation的构造函数
	//我想好这个功能我应该能接受这么几个东西？
	//1. 接受单个 :$("id").validation({type:'email',error_info:{}})
	//2. 接受一个表单 $("form").validation([{id:'',type{}},{id:'',type{}},{id:'',type:{}}])
	//现在最重要的type {}参数的配置
	//{type:'text',defaultReg:'',restrictions:{},error_info{container:'',message:'',css:{}}}
	var _Reg= {
		_EMAIL:'email',
		_PHONE:'phone',
	}
	var Validation =function (type,$this){
		//单个不需要提供任何的内容我们会自己实现
		this.obj = $this;
		if(type.type == 'text'){
			this.type =type.type ;
			this.event = type.event;
			this.defaultReg = type.defaultReg;
			this.restrictions = type.restrictions;
			this.error_info = type.info;
			this.remote = type.remote;
		}else{
			throw '暂时不支持';
		}
	} 
	Validation.base_validation = {
		_validationLength:function(value,restrictions){
			if(typeof restrictions.max != 'undefined'){
				if(value.length<restrictions.max){
					if(typeof restrictions.min != 'undefined'){
						if(value.length>restrictions.min){
							return "ok"
						}else{
							return "长度不能小于"+restrictions.min;
						}
					}
				}else{
					return "长度不能超过"+restrictions.max;
				}
			}else if(typeof restrictions.min != 'undefined'){
				if(value.length>restrictions.min){
					return "ok"
				}else{
					return "长度不能小于"+restrictions.min;
				}
			}
			return "ok";
		},
		_validationNotNull:function(value,restrictions){
			if(typeof restrictions.notNull !='undefined' && restrictions.notNull == false){
				return 'ok';
			}
			if(value!=null&&value!=''){
				return 'ok'
			}else{
				return "不能为空";
			}
		},
		_validationNotNumber:function(value,restrictions){
			if(typeof restrictions.notNumber !='undefined' && restrictions.notNumber==true){
				var pattern = /^\s*\d+\s*$/g;
				if(pattern.test(value)){
					return 'ok';
				}else{
					return '必须全部为数字';
				}
			}else{
				return 'ok';
			}
		},
		_validationContainerSignal:function(value,restrictions){
			if(typeof restrictions.notSignal !='undefined' && restrictions.notSignal==true){
				var pattern = /[^a-zA-Z0-9\f\n\r\t\v]/;
				if(pattern.test(value)){
					return 'ok';
				}else{
					return '不能包含字符';
				
				}
			}else{
				return 'ok';
			}
		},
		//这个函数提供一个队列用于将所有执行函数推送进去一个执行不成功那么后面的全部不执行
		//清空该队列  不想自己写了还是用jquery的吧
		validationPush:function(value,restrictions){
			var queue  = [];
			queue.push(this._validationLength);
			queue.push(this._validationNotNull);
			queue.push(this._validationNotNumber);
			queue.push(this._validationContainerSignal);
			//出队列然后最后得到最后的值
			var temp_info = null;
			while(queue.length){
				temp_info =queue.shift().call(this,value,restrictions);
				if(temp_info=='ok')
					continue;
				else
					return temp_info;
			}
			return temp_info;
		}
	}
	//文本模式下的验证
	Validation.text_validation = {
		base:Validation.base_validation,
		validateReg:function(defaultReg,message,$this){
			var value = $this.val();
			if(typeof defaultReg == 'string'&& defaultReg ==_Reg._EMAIL){
				return this._emailReg(value,message);
			}else if(typeof defaultReg == 'string'&& defaultReg==_Reg._PHONE){
				return this._phoneReg(value,message);
			}else if(typeof defaultReg == 'object'){
				//自定义的执行正则对象
				return this._defineReg(defaultReg,value,message);
			}
		},
		validateRestriction:function(restrictions,$this){
			var value = $this.val();
			//这里想了一下如果说我是一个一个跑肯定有问题所以跑一个链条的跑
			return this.base.validationPush(value,restrictions);
		},
		_phoneReg:function(value,message){
			var defaultReg = /^\s*1\d{10}\s*$/;
			return this._defineReg(defaultReg,value,typeof message == 'string'?message:'电话号格式不正确');
		},
		_emailReg:function(value,message){
			var defaultReg = /^\s*\w+@\w+\.(com|cn)(\s*|(\.(com|cn|org)\s*))$/;
			return this._defineReg(defaultReg,value,typeof message == 'string'?message:'电话号格式不正确');
		},
		_defineReg:function(defaultReg,value,message){
			if(typeof defaultReg == 'object'){	
				if(defaultReg.test(value)){
					return 'ok';
				}else{
					return message;
				}
			}
		}
	
	}
	Validation.button_validation = {

	}
	//只实现了text其他没有
	Validation.validationType = {
		text:Validation.text_validation,
		button:Validation.button_validation
	}
	Validation.Remote = {
		remoteConfig:{},
		result:{},
		requestAjax:function(name,value){
			var re  = result;
			var config = this.remoteConfig;
			var data = 
			typeof config.dataFormat == 'function'?
			config.dataFormat(name,value):
			'{'+name+':'+value+'}';
			$.ajax({
				type:'post',
				contentType:'application/json'
				dataType:'json',
				data:data,
				url:this.remoteUrl,
				sync:false,
				success:function(data){
					if(typeof config.success!='function'){
						//自动处理在没有success函数的时候
						$container= $("#"+config.container);
						re.message = data.message;
						re.status = data.status;
					}else{
						//自定义处理
						config.success(data,re,$("#"+config.container));
					}
				},
				error:function(){
					config.error();
				}
			});
		}
	}
	Validation.prototype = {
		flush:function(){
			return this.validateOps();
		},
		validateOps:function(){
			return this._validate();
		},
		_validate:function(){
			var restriction_error = Validation.validationType[this.type].validateRestriction(this.restrictions,this.obj);
			var defaultReg_error = Validation.validationType[this.type].validateReg(this.defaultReg,this.error_info.message,this.obj);
			Validation.Remote.remoteConfig = this.remote;
			Validation.Remote.remoteConfig.container = this.error_info.container;
			//通过者2个错误一起判断
			if(restriction_error=='ok'){
				if(defaultReg_error == 'ok'){
					//去请求远端如果请求不到
					Validation.Remote.requestAjax(this.obj.attr('name'),this.obj.val());
					if(Validation.Remote.result.status==1){
						this._showError(1,Validation.Remote.result.message);
					}else{
						this._showError(0,Validation.Remote.result.message);
					}
					return true;
				}else{
					this._showError(0,defaultReg_error);
					return false;
				}
			}else{
				this._showError(0,restriction_error);
				return false;
			}
		},
		_showError:function(status,info){
			var $container = null;
			if(typeof this.error_info.container!='string') 
				return ; 
			var con = null;
			var css = this.error_info.css;
			if(status == 1){
				css =  this.error_info.success_css;
			}
			if(typeof info=='string')
				con = info;
			else
				con = success;
			$container= $("#"+this.error_info.container);
			$container.css(typeof css == 'object'?css:{});
			$container.text(con);
		}
	}

	//函数的默认参数
	Validation.defaults= {

	}
	//option来判断请求的是什么？
	//arg来传递函数的参数等等
	var Pugin = function(option,arg){
		options = $.extend({},Validation.defaults,typeof option == 'object'?option:{});
		if(this.length==1&&option == 'status') {
			console.log($(this[0]));
			return $(this[0]).data('status');
		}else if(this.length>1){
			throw '判断validate_status时只能使用一个结点进行判断';
		}
		if(this[0].nodeName == 'FORM'){
			//这里需要去跑他的子结点了 这是扩展的线实现最简单的 
			return ;
		}
		//执行某个对象的参数
		return this.each(function(){
			var $this = $(this);
			//判断该对象是否已经被绑定验证
			if(typeof  $this.data('dataObj')=='object'){
				var data =  $this.data('dataObj');
				if(typeof option == 'string'){
					$this.data('status',data[option](arg));
				}
				return;
			}
			//第一次进来将new Validation绑定到data上后面
			//来直接去的该结点上对象进行函数操作
			//但是这里不需要因为验证嘛我只需要把类型什么的都写好输入就行了
			var temp = new Validation(options,$this);
			var status = temp.validateOps();
			$this.data('dataObj',temp);
			$this.data('status',status);
		})
	}
	$.fn.validation = Pugin;
	$.fn.validation.Contructor =  Validation;
})(jQuery);